local p = {}
-- 通用
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')
local character_map = require('Module:Util/Character')
local ELEMENT_MAP = require('Module:Util/Config').ELEMENT_MAP

---------------------------------------------------------------------------
-- 常量
---------------------------------------------------------------------------

local TYPE_LIST = {'火', '水', '地', '风', '光', '暗', '可变化', '紫薇'}

local TYPE_SET_MAP = {
	['火'] = {s='#FF6565', t='火属性'},
	['水'] = {s='#69A9FF', t='水属性'},
	['地'] = {s='#C88018', t='地属性'},
	['风'] = {s='#77EE88', t='风属性'},
	['光'] = {s='#EACC01', t='光属性'},
	['暗'] = {s='#B091FF', t='暗属性'},
	['可变化'] = {s='', t='可变化属性'},
	['紫薇'] = {s='#EEEEEE', t='紫薇星垣'},
}

---------------------------------------------------------------------------
-- 获取数据
---------------------------------------------------------------------------

function p.query_data(rarity)
	-- 查询数据

	local result = character_map

	-- 按属性整理数据
	local output = {}
	for _, element in ipairs(TYPE_LIST) do
		output[element] = {}
	end

	for _, char_info in pairs(character_map) do
	    char_info.ID = _
		if (char_info.addDate or '') ~= '' and (tonumber(char_info.ID) or 0) > 0 then
			if (tonumber(char_info.rarity) or '') == 4 then
				table.insert(output['紫薇'], char_info)
			elseif (rarity or '') == '' or (char_info.rarity == rarity) then
				table.insert(output[ELEMENT_MAP[char_info.element]], char_info)
			end
		end
	end
	
	for _, element in ipairs(TYPE_LIST) do
		table.sort(output[element], p.sort_char_data)
	end

	return output
end

function p.sort_char_data(a, b)
	if (a.rarity ~= b.rarity) then
		return a.rarity > b.rarity
	end
    if (a.addDate ~= b.addDate) then
		return a.addDate < b.addDate
	end
	return a.ID < b.ID
end

function p.reverseTable(tab)
	local tmp = {}
	for i = 1, #tab do
		local key = #tab
		tmp[i] = table.remove(tab)
	end

	return tmp
end

-----------------------------------------------------------------------------
-- 角色信息页生成
-----------------------------------------------------------------------------

function p.char_list(frame)
	local args = getArgs(frame)

	local picture = args[1] or '01'
	local rarity = args[2] or ''
	local picture_l = picture
	local bool = true
	local char_data = p.query_data(rarity)

	local html = mw.html.create()

	for _, element in ipairs(TYPE_LIST) do
		if #char_data[element] > 0 then
			local type_info = TYPE_SET_MAP[element]

			local h2 = html:tag('h2'):cssText('font-size:24px;'):addClass('shadow-text'):wikitext(type_info.t)
			if type_info.s ~= '' then
				h2:cssText('color:'..type_info.s)
			end

			local flex_list = html:tag('div'):addClass('flex-list m-flex-list'):cssText('text-align: center;font-size:14px;')

			for _, char_info in ipairs(char_data[element]) do
				local link = 'Char/'..char_info.ID
				--local auto_element_text = char_info.element == '可变化' and '_02' or ''  -- 可变属性角色
				local auto_element_text = ''
				bool = true
				--[[
				if picture == '03' then
					if char_info.max_evo < 5 then
						bool = false
					else
						if (char_info.char_count or 0) > 0 then
							picture_l = '03_101'
						else
							picture_l = '03'
						end
					end
				elseif picture == '81' then
					if (char_info.extra_img[1] or '') == '' then
						bool = false
					else
						picture_l = char_info.extra_img[1]
					end
				end
				--]]
				if bool then
                    local name = char_info.name
					if char_info.nickname ~= '' then
						name = char_info.nickname
					end
					flex_list:tag('div'):addClass('flex-item char-box')					
					:tag('div'):wikitext('[[File:s_'..char_info.ID..'_'..picture_l..auto_element_text..'.png|link='..link..']]'):done()
					:tag('div'):wikitext('[['..link..'|'..(name or '')..']]'):done()
				end
			end
		end
	end

	return html

	-- body
end


return p
